<?php

/**
 * Description of ContentEmployeeBooks
 *
 * @author Honza, Petr
 */
class ContentEmployeeBooks implements IMainBodyWithInteraction {

  private $showTable = true;
  private $showForm = true;
  private $error;

  public function action() {
  	//delete all related informations
    if ($_GET['a'] == 'erase') {
    	//delete relation with authors
      DB::sql('delete from ' . TBL_AUTHORS_BOOKS . ' where id_book = ' . $_GET['ida']);
      	//delete relations with gengre of book
      DB::sql('delete from ' . TBL_GENRES_BOOKS . ' where id_book = ' . $_GET['ida']);
      	//delete from books table
      DB::sql('delete from ' . TBL_BOOKS . ' where id_book = ' . $_GET['ida']);
    }

    if ($_GET['a'] == 'detail') {
      $this->showTable = false;

      $bookDetail = new ContentBook($_GET['ida']);
      return $bookDetail->getContent();
    }
  }

  public function applyForm($formID = NULL) {
    //EDIT BOOK
    if (isset($_POST['editBook'])) { 
    	//assign variables
      $ida = $_POST['ida'];
      $title = addslashes($_POST['title']);
      $authors = $_POST['authors'];
      $language = $_POST['lang'];
      $isbn = $_POST['isbn'];
      $pages = $_POST['pages'];
      $imprints = $_POST['imprints'];
      $edition = $_POST['edition'];
      $genres = $_POST['genres'];
      $description = $_POST['desc'];

      if (!$title || !$genres || !$authors || !is_numeric($pages) || !is_numeric($edition)) {
        //error
        $this->error = '<p class="error">Title, author and genre has to be filled. Pages and edition has to be a number.</p>';
      } else {
        //OK
        $this->showForm = false;
        $this->showTable = true;
        
        $sql = 'update ' . TBL_BOOKS . " set id_language = $language, title='$title',isbn='$isbn', pages=$pages, imprint='$imprints', edition='$edition', description='$description'";
        $sql .= ' where id_book = ' . $ida;
        DB::sql($sql);

        //authors
        DB::sql('delete from ' . TBL_AUTHORS_BOOKS . ' where id_book = ' . $ida);
        foreach ($authors as $authorID) {
          //echo $idAutora . BR;
          

          $sql = 'insert into ' . TBL_AUTHORS_BOOKS . " values($ida,$authorID)";
          DB::sql($sql);
        }

        //gengre
        DB::sql('delete from ' . TBL_GENRES_BOOKS . ' where id_book = ' . $ida);
        foreach ($genres as $genreID) {
          
          $sql = 'insert into ' . TBL_GENRES_BOOKS . " values($genreID, $ida)";
          DB::sql($sql);
        }
      }
    }

    //CREATE BOOK
    if (isset($_POST['createBook'])) {
      $title = addslashes($_POST['title']);
      $authors = $_POST['authors'];
      $language = $_POST['lang'];
      $isbn = $_POST['isbn'];
      $pages = $_POST['pages'];
      $imprints = $_POST['imprints'];
      $edition = $_POST['edition'];
      $genres = $_POST['genres'];
      $description = $_POST['desc'];

      if (!$genres || !$authors || !is_numeric($pages) || !is_numeric($edition)) {
        //error
        $this->error = '<p class="error">Author and genre has to be filled. Pages and edition has to be a number.</p>';
      } else {
        //v poradku

        $this->showForm = false;
        $this->showTable = true;
        $sql = 'insert into ' . TBL_BOOKS . " values(0,'$language','$title','$isbn',$pages,'$imprints',$edition,'$description')";
        $bookID = DB::sql($sql, 'id_book');

        //authors
        foreach ($authors as $authorID) {
          //create a row in table authors_books
          $sql = 'insert into ' . TBL_AUTHORS_BOOKS . " values($bookID,$authorID)";
          DB::sql($sql);
        }

        //genres
        foreach ($genres as $genreID) {
          //create a row in table genres_books
          $sql = 'insert into ' . TBL_GENRES_BOOKS . " values($genreID, $bookID)";
          DB::sql($sql);
        }
      }
    }

    //ADD BOOKPRINTS
    if (isset($_POST['addBookprint'])) {
      $count = addslashes($_POST['count']);
      $location = addslashes($_POST['location']);
      $bookID = addslashes($_POST['ida']);

      if (!is_numeric($count)) {
        $this->error = '<p class="error">Count has to be a numeric.</p>';
      }

      for ($i = 0; $i < $count; $i++) {
        $sql = 'insert into ' . TBL_BOOK_PRINTS . " values (0, $bookID, $location, CURDATE( ), NULL, 0, NULL)";
        $bookPrintID = DB::sql($sql, 'id_book_print');
        $sql = 'update ' . TBL_BOOK_PRINTS . ' set barcode = ' . $bookPrintID . ' where id_book_print = ' . $bookPrintID;
        DB::sql($sql);
      }
      $this->showForm = false;
    }
  }

  public function getContent() {
    $content .= '<h2>Books</h2>';
    
    $content .= $this->action();
    $this->applyForm();
    
    $form = $this->getForm();
    if ($this->showTable) {
      $content .= '<a href="index.php?where=employee&subwhere=books&a=create">Create book</a>';
    }
    $content .= $form;
    $content .= $this->getTable();

    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
    if ($_GET['a'] == 'create' && $this->showForm) {
      // form for creating a book
      $this->showTable = false;

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Title', 'title')->addInput('text', 'title', $_POST['title']);
      $sql = 'select concat(surname, \', \', name) as authors, id_author from ' . TBL_AUTHORS . ' order by surname';
      $f->addLabel('Authors', 'authors')->addSelectionBox('authors', DB::sql($sql), 'authors', 'id_author', $_POST['authors'], 5, true);
      $sql = 'select * from ' . TBL_LANGUAGES . ' order by name';
      $f->addLabel('Language', 'lang')->addSelectionBox('lang', DB::sql($sql), 'name', 'id_language', $POST['lang']);
      $f->addLabel('ISBN', 'isbn')->addInput('text', 'isbn', $_POST['isbn']);
      $f->addLabel('Pages', 'pages')->addInput('text', 'pages', $_POST['pages']);
      $f->addLabel('Imprints', 'imprints')->addInput('text', 'imprints', $_POST['imprints']);
      $f->addLabel('Edition', 'edition')->addInput('text', 'edition', $_POST['edition']);
      $f->addLabel('Description', 'desc')->addTextArea('desc', $_POST['desc']);

      $sql = 'select * from ' . TBL_GENRES . ' order by name';
      $f->addLabel('Genres', 'genres')->addSelectionBox('genres', DB::sql($sql), 'name', 'id_genre', $_POST['genres'], 5, true);

      $f->addLabel('&nbsp', 'createBook')->addInput('submit', 'createBook', 'Create a book', array("class" => "submit"));
      $f->endForm();
      return $this->error . $f->getForm();
    }
    //EDIT THE BOOK
    if ($_GET['a'] == 'edit' && $this->showForm) {
      $this->showTable = false;

      //form for book editing
      if (!isset($_POST['editBook'])) {
        $ida = $_GET['ida'];
        
        //gathering data for form
        $sql = 'select * from ' . TBL_BOOKS . ' where id_book = ' . $ida;
        $kniha = DB::sql($sql);

        $sql = 'select * from ' . TBL_AUTHORS_BOOKS;
        $sql .= ' where id_book = ' . $ida;
        $authorsDB = DB::sql($sql);
        $authors = array();
        foreach ($authorsDB as $author) {
          $authors[] = $author['id_author'];
        }

        $sql = 'select id_genre from ' . TBL_GENRES_BOOKS;
        $sql .= ' where id_book = ' . $ida;
        $genresDB = DB::sql($sql);
        $genres = array();
        foreach ($genresDB as $genre) {
          $genres[] = $genre['id_genre'];
        }


        $title = $kniha[0]['title'];
        $language = $kniha[0]['id_language'];
        $isbn = $kniha[0]['isbn'];
        $pages = $kniha[0]['pages'];
        $imprints = $kniha[0]['imprint'];
        $edition = $kniha[0]['edition'];

        $description = $kniha[0]['description'];
      } else {
        $title = addslashes($_POST['title']);
        $authors = $_POST['authors'];
        $language = $_POST['lang'];
        $isbn = $_POST['isbn'];
        $pages = $_POST['pages'];
        $imprints = $_POST['imprints'];
        $edition = $_POST['edition'];
        $genres = $_POST['genres'];
        $description = $_POST['desc'];
      }

      //draw form for edit the book
      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Title', 'title')->addInput('text', 'title', $title);
      $sql = 'select concat(surname, \', \', name) as author, id_author from ' . TBL_AUTHORS . ' order by surname';
      $f->addLabel('Authors', 'authors')->addSelectionBox('authors', DB::sql($sql), 'author', 'id_author', $authors, 5, true);
      $sql = 'select * from ' . TBL_LANGUAGES . ' order by name';
      $f->addLabel('Language', 'lang')->addSelectionBox('lang', DB::sql($sql), 'name', 'id_language', $language);
      $f->addLabel('ISBN', 'isbn')->addInput('text', 'isbn', $isbn);
      $f->addLabel('Pages', 'pages')->addInput('text', 'pages', $pages);
      $f->addLabel('Imprints', 'imprints')->addInput('text', 'imprints', $imprints);
      $f->addLabel('Edition', 'edition')->addInput('text', 'edition', $edition);
      $f->addInput('hidden', 'ida', $ida);
      $f->addLabel('Description', 'desc')->addTextArea('desc', $description);

      $sql = 'select * from ' . TBL_GENRES . ' order by name';
      $f->addLabel('Genres', 'genres')->addSelectionBox('genres', DB::sql($sql), 'name', 'id_genre', $genres, 5, true);

      $f->addLabel('&nbsp', 'editBook')->addInput('submit', 'editBook', 'Edit a book', array("class" => "submit"));
      $f->endForm();
      return $this->error . $f->getForm();
    }

    if ($_GET['a'] == 'bookprints' && $this->showForm) {
      //form for adding bookprints
      $this->showTable = false;
      $bookID = addslashes($_GET['ida']);

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Quantity', 'count')->addInput('text', 'count', $_POST['count']);
      $sql = 'select * from ' . TBL_LOCATIONS . ' order by name';
      $f->addLabel('Location', 'location')->addSelectionBox('location', DB::sql($sql), 'name', 'id_location', $_POST['location'], 1, false);
      $f->addInput('hidden', 'ida', $bookID);
      $f->addLabel('&nbsp', 'addBookprint')->addInput('submit', 'addBookprint', 'Add bookprints', array("class" => "submit"));
      $f->endForm();

      return $f->getForm();
    }
  }

  public function getSubmitedForm() {
    
  }

  public function getTable($tableID = NULL) {
  	//return table with books
    if ($this->showTable) {
      //table of all books
      $sql = 'select id_book, title, concat(name, \' \', surname) as autor from ' . TBL_BOOKS . '';
      $sql .= ' join ' . TBL_AUTHORS_BOOKS . ' using (id_book)';
      $sql .= ' join ' . TBL_AUTHORS . ' using (id_author)';
      $sql .= ' order by title';

      $result = DB::sql($sql); 

      if (empty($result)) {
        return '<p>No books</p>';
      }

      $lastID = -1; //stop with duplicity

      //draw table with data
      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Title</th><th>Author</th><th>Details</th><th>Edit</th><th>Add bookprints</th><th>Erase</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $idBook = $row['id_book'];

        if ($idBook != $lastID) {
          $lastID = $idBook;
          $table .= '<tr>';
          $table .= '<td>' . $row['title'] . '</td>';
          $table .= '<td>' . $row['autor'] . '</td>';
          $table .= '<td><a href="index.php?where=employee&subwhere=books&a=detail&ida=' . $idBook . '">Details</a></td>';
          $table .= '<td><a href="index.php?where=employee&subwhere=books&a=edit&ida=' . $idBook . '">Edit</a></td>';
          $table .= '<td><a href="index.php?where=employee&subwhere=books&a=bookprints&ida=' . $idBook . '">Add bookprints</a></td>';
          $table .= '<td><a href="index.php?where=employee&subwhere=books&a=erase&ida=' . $idBook . '">Erase</a></td>';
          $table .= '</tr>';
        }
      }
      $table .= '';
      $table .= '</table>';

      return $table;
    }
  }

}

?>
